package com.CloudDream.Util;

import com.CloudDream.Item.Creator;
import com.CloudDream.MoonCake;
import com.cryptomorin.xseries.XMaterial;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.bukkit.SkullType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import sun.misc.BASE64Encoder;

import java.lang.reflect.Field;
import java.util.List;
import java.util.UUID;

public class CustomHead {

    private static final UUID UUID = null;
    private static MoonCake instance = MoonCake.getInstance();
    final BASE64Encoder encoder = new BASE64Encoder();

    public ItemStack getSkull(String url, String Displayname, List<String> lore, String mark) {

        ItemStack head = new Creator(XMaterial.SKELETON_SKULL.parseMaterial(), Displayname, lore, mark, (short) SkullType.PLAYER.ordinal(), false).get();

        if (url.isEmpty()) return head;

        SkullMeta headMeta = (SkullMeta) head.getItemMeta();
        GameProfile profile = new GameProfile(UUID.randomUUID(), null);
        String encodedData = encoder.encode(String.format("{textures:{SKIN:{url:\"%s\"}}}", "http://textures.minecraft.net/texture/" + url).getBytes());
        profile.getProperties().put("textures", new Property("textures", encodedData));
        Field profileField = null;
        try {
            profileField = headMeta.getClass().getDeclaredField("profile");
            profileField.setAccessible(true);
            profileField.set(headMeta, profile);
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) {
            e1.printStackTrace();
        }
        head.setItemMeta(headMeta);
        return head;
    }
}
